﻿using MongoDB.Model;
using Quartz.Impl;
using Quartz.Spi;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace Quartz.Service
{
    public class QuartzStartup : IQuartzStartup
    {
        //声明一个调度工厂
        private readonly ISchedulerFactory _schedulerFactory;
        private readonly IJobFactory _jobFactory;
        private IScheduler _scheduler;
        private static Dictionary<string, JobKey> JobManage;
        public QuartzStartup(IJobFactory jobFactory, ISchedulerFactory schedulerFactory)
        {
            JobManage = new Dictionary<string, JobKey>();
            _jobFactory = jobFactory;
            _schedulerFactory = schedulerFactory;
        }

        /// <summary>
        /// Quartz启动
        /// </summary>
        /// <returns></returns>
        public async Task Start(CancellationToken cancellationToken)
        {
            //通过调度工厂获得调度器
            _scheduler = await _schedulerFactory.GetScheduler(cancellationToken);
            _scheduler.JobFactory = _jobFactory;
            //开启调度器
            await _scheduler.Start(cancellationToken);
        }

        /// <summary>
        /// 添加一个定时任务
        /// </summary>
        /// <param name="Task"></param>
        /// <returns></returns>
        public async Task AddJob(Mongo_TaskManageModel Task)
        {
            //创建一个触发器
            var trigger = TriggerBuilder.Create()
                            .WithCronSchedule(Task.TaskCron)
                            .Build();
            //绑定任务参数
            JobDataMap DataMap = new JobDataMap();
            DataMap.Add("guid", Task.guid);
            //创建任务
            var jobDetail = JobBuilder.Create<QuartzJob>()
                            .WithIdentity(Task.TaskName, Task.GroupName)
                            .UsingJobData(DataMap)
                            .Build();
            //将触发器和任务器绑定到调度器中
            await _scheduler.ScheduleJob(jobDetail, trigger);
            //讲Key保存起来，便于进行管理
            JobManage.Add(Task.guid, jobDetail.Key);
        }

        /// <summary>
        /// 删除定时任务
        /// </summary>
        /// <param name="guid"></param>
        /// <returns></returns>
        public async Task DeleteTask(string guid)
        {
            if (JobManage.Keys.Contains(guid))
            {
                var jobKey = JobManage[guid];
                await _scheduler.PauseJob(jobKey);
                await _scheduler.DeleteJob(jobKey);
                JobManage.Remove(guid);
            }
        }

        /// <summary>
        /// 暂停定时任务
        /// </summary>
        /// <returns></returns>
        public async Task PauseTask(string guid)
        {
            if (JobManage.Keys.Contains(guid))
            {
                await _scheduler.PauseJob(JobManage[guid]);
            }
        }

        /// <summary>
        /// 恢复定时任务
        /// </summary>
        /// <param name="TaskName"></param>
        /// <returns></returns>
        public async Task ResumeTask(string guid)
        {
            if (JobManage.Keys.Contains(guid))
            {
                await _scheduler.ResumeJob(JobManage[guid]);
            }
        }

        /// <summary>
        /// 关闭所有的定时任务
        /// </summary>
        /// <returns></returns>
        public async Task Clear()
        {
            await _scheduler.Clear();
        }
    }
}
